package com.File_io_.io_.OutputStream.ObjectOutputStream;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 使用ObjectOutputStream 进行序列化
 */
public class ObjectOutputStream_ {
    public static void main(String[] args) throws Exception {
        //序列化后保存的文件格式不是文本文件格式，而是按照系统格式来保存
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\idea_WorkSpace\\myjava\\myjava\\src\\com\\File_io_\\date\\date.dat"));
        //序列化数据到D:\idea_WorkSpace\myjava\myjava\src\com\File_io_\date\date.txt
        oos.writeInt(1000);
        oos.writeBoolean(true);
        oos.writeChar('a');
        oos.writeUTF("风雨之后");
        oos.writeObject(new Dog("aaa",2));
        oos.close();
    }
}
//如果需要序列化某个类的对象，需要实现Serializable接口
class Dog implements Serializable {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
